<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地区名称" prop="areaTitle">
        <el-input
          v-model="queryParams.areaTitle"
          placeholder="请输入地区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:area:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="areaList"
      row-key="areaId"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="地区名称" align="left" prop="areaTitle" />
      <el-table-column label="地区类型" align="center" prop="areaType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.area_type" :value="scope.row.areaType"/>
        </template>
      </el-table-column>
      <el-table-column label="地区排序" align="center" prop="areaOrder" />
      <el-table-column label="创建时间" align="center" prop="areaCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.areaCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:area:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['business:area:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:area:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改地区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="地区ID" prop="areaId" v-show="false">
          <el-input v-model="form.areaId" placeholder="请输入地区ID" />
        </el-form-item>
        <el-form-item label="上一级地区" prop="areaFatherId">
          <treeselect v-model="form.areaFatherId" :options="areaOptions" :normalizer="normalizer" placeholder="请选择上一级地区"  :disable-branch-nodes="true"/>
        </el-form-item>
        <el-form-item label="地区名称" prop="areaTitle">
          <el-input v-model="form.areaTitle" placeholder="请输入地区名称" />
        </el-form-item>
        <el-form-item label="地区类型" prop="areaType">
          <el-select v-model="form.areaType" placeholder="请选择地区类型">
            <el-option
              v-for="dict in areaTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区排序" prop="areaOrder">
          <el-input v-model="form.areaOrder" placeholder="请输入地区排序序号" />
        </el-form-item>
        <el-form-item label="备注" prop="areaRemark">
          <el-input v-model="form.areaRemark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArea, getArea, delArea, addArea, updateArea, exportArea } from "@/api/business/area";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Area",
  dicts: ['area_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 地区表格数据
      areaList: [],
      // 地区树选项
      areaOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 地区类型字典
      areaTypeOptions: [],
      // 审批状态字典
      areaStateOptions: [],
      // 查询参数
      queryParams: {
        areaId: null,
        areaFatherId: null,
        areaTitle: null,
        areaState: null,
        areaCreateTime: null,
        areaCreateBy: null,
        areaUpdateBy: null,
        areaApproveBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaFatherId: [
          { required: true, message: "父地区ID不能为空", trigger: "blur" }
        ],
        areaTitle: [
          { required: true, message: "地区名称不能为空", trigger: "blur" }
        ],
        areaType: [
          { required: true, message: "地区类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("area_type").then(response => {
      this.areaTypeOptions = response.data;
    });
    this.getDicts("sys_verify_type").then(response => {
      this.areaStateOptions = response.data;
    });
  },
  methods: {
    /** 查询地区列表 */
    getList() {
      this.loading = true;
      listArea(this.queryParams).then(response => {
        this.areaList = this.handleTree(response.data, "areaId", "areaFatherId");
        this.loading = false;
      });
    },
    /** 转换地区数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.areaTitle,
        children: node.children
      };
    },
    /** 查询地区下拉树结构 */
    getTreeselect() {
      listArea().then(response => {
        this.areaOptions = [];
        const data = { areaId: 0, areaTitle: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "areaId", "areaFatherId");
        this.areaOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        areaId: null,
        areaFatherId: null,
        areaTitle: null,
        areaShortTitle: null,
        areaType: null,
        areaOrder: null,
        areaRemark: null,
        areaState: null,
        areaCreateId: null,
        areaUpdateId: null,
        areaCreateTime: null,
        areaCreateBy: null,
        areaUpdateTime: null,
        areaApproveId: null,
        areaApproveTime: null,
        areaUpdateBy: null,
        areaApproveBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.areaId) {
        this.form.areaFatherId = row.areaId;
      } else {
        this.form.areaFatherId = 0;
      }
      this.open = true;
      this.title = "添加地区";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.areaFatherId = row.areaId;
      }
      getArea(row.areaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地区";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.areaId != null) {
            updateArea(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除地区编号为"' + row.areaId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delArea(row.areaId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
